package com.mytech.cslive.activities;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;
import android.widget.ToggleButton;

import com.mytech.cslive.Player;
import com.mytech.cslive.Point2D;
import com.mytech.cslive.R;
import com.mytech.cslive.infrastructure.AppConfig;
import com.mytech.cslive.infrastructure.LogFile;

public class SettingsActivity extends Activity {
	private boolean isLocationServiceOn;
	
	@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_settings);
        
        isLocationServiceOn = false;
	}
	
	@Override
	protected void onStart() {
		super.onStart();
		
		this.showAppConfigValues();
		this.restoreState();
	}
	
	@Override
	protected void onPause() {
		super.onPause();
		
		this.saveState();
	}
	
	public boolean isLocationServiceOn() {
		return isLocationServiceOn;
	}

	public void setLocationServiceOn(boolean isLocationServiceOn) {
		this.isLocationServiceOn = isLocationServiceOn;
	}

	private void restoreState() {
		ToggleButton startLocationServiceButton = 
				(ToggleButton) findViewById(R.id.start_location_service_button);
		
		Boolean isLocationServiceOn = (Boolean)
				IntentHelper.getObjectForKey("IsLocationServiceOn");
		
		if (isLocationServiceOn != null && isLocationServiceOn == true) {
			startLocationServiceButton.toggle();
			
			this.setLocationServiceOn(isLocationServiceOn);
		}
	}
	
	private void saveState() {
		// Save state
		IntentHelper.addObjectForKey("IsLocationServiceOn", 
				this.isLocationServiceOn());
	}
	
	private void showAppConfigValues() {
		String blueCommanderIP = 
				AppConfig.getProperty(Player.getPropertyCommanderIpBlue());
		String redCommanderIP = 
				AppConfig.getProperty(Player.getPropertyCommanderIpRed());
		String spawnPointLocationString =
				AppConfig.getProperty(Player.getPropertySpawnPointLocation());
		String myFlagLocationString =  
				AppConfig.getProperty(Player.getPropertyMyFlagLocation());
		
		this.setTextToTextField(R.id.blue_commander_ip_text, blueCommanderIP);
		this.setTextToTextField(R.id.red_commander_ip_text, redCommanderIP);
		
		this.showPoint(R.id.spawn_point_location_x_text, R.id.spawn_point_location_y_text, 
				spawnPointLocationString);
		this.showPoint(R.id.my_flag_location_x_text, R.id.my_flag_location_y_text, 
				myFlagLocationString);
	}
	
	private void setTextToTextField(int id, String text) {
		if (text != null) {
			  EditText editText = 
					  (EditText) findViewById(id);
			  editText.setText(text);
		}
	}
	
	private void showPoint(int idX, int idY, Point2D point) {
		if (point != null) {	
			this.setTextToTextField(idX, Double.toString(point.x));
			this.setTextToTextField(idY, Double.toString(point.y));
		} else {
			String message = "Point in showPoint is null";
			
			LogFile.log(message);
			
		     // Show a message to the user
		     Toast.makeText(this,
		    		 message,
		    		 Toast.LENGTH_SHORT).show();
		}
	}
	
	private void showPoint(int idX, int idY, String pointLocationString)  {
		if (pointLocationString != null) {
			Point2D point = new Point2D();
			
			try {
				point.parse(pointLocationString);
				
				this.showPoint(idX, idY, point);
			} catch (NumberFormatException ex) {
				LogFile.log(ex);
				
			     // Show a message to the user
			     Toast.makeText(this,
			    		 ex.getMessage(),
			    		 Toast.LENGTH_SHORT).show();
			}
		}
	}
	
	private String getTextFromTextField(int id) {
		  EditText editText = 
				  (EditText) findViewById(id);
		  return (editText.getText().toString());
	}
	
	public void onSettingsButtonClicked(View v) {
		 String blueCommanderIP = 
				 this.getTextFromTextField(R.id.blue_commander_ip_text);
		 String redCommanderIP = 
				 this.getTextFromTextField(R.id.red_commander_ip_text);
		 
		 String spawnPointLocationX = 
				 this.getTextFromTextField(R.id.spawn_point_location_x_text);
		 String spawnPointLocationY = 
				 this.getTextFromTextField(R.id.spawn_point_location_y_text);
		 String myFlagLocationX = 
				 this.getTextFromTextField(R.id.my_flag_location_x_text);
		 String myFlagLocationY = 
				 this.getTextFromTextField(R.id.my_flag_location_y_text);
		 
	     AppConfig.setNonEmptyProperty(
					 Player.getPropertyCommanderIpBlue(), blueCommanderIP);
	     AppConfig.setNonEmptyProperty(
					 Player.getPropertyCommanderIpRed(), redCommanderIP);
	     
	     try {
		     AppConfig.setNonEmptyProperty(
						 Player.getPropertySpawnPointLocation(), 
						 new Point2D(spawnPointLocationX, spawnPointLocationY).toString());
		     AppConfig.setNonEmptyProperty(
					 Player.getPropertyMyFlagLocation(), 
					 new Point2D(myFlagLocationX, myFlagLocationY).toString());
		     
		     // Show a message to the user
		     Toast.makeText(this,
		    		 "Settings saved successfully!",
		    		 Toast.LENGTH_SHORT).show();
		     
		     this.finish();
	     } catch (NumberFormatException ex) {
	    	 LogFile.log(ex);
	    	 
		     // Show a message to the user
		     Toast.makeText(this,
		    		 "Location axis must consist numbers!",
		    		 Toast.LENGTH_SHORT).show();
	     }
	}
	
	public void onSetCurrentLocationSpawnPoint(View v) {
		Player player = (Player) IntentHelper.getObjectForKey(MainActivity.getPropertyPlayer());
		
		this.showPoint(R.id.spawn_point_location_x_text, R.id.spawn_point_location_y_text,
				player.getCurrentLocation());
	}
	
	public void onSetCurrentLocationMyFlag(View v) {
		Player player = (Player) IntentHelper.getObjectForKey(MainActivity.getPropertyPlayer());
		
		this.showPoint(R.id.my_flag_location_x_text, R.id.my_flag_location_y_text,
				player.getCurrentLocation());
	}
	
	public void onToggleLocationService(View v) {
		if (isLocationServiceOn()) {
			MainActivity.shutdownLocationService(this);
			setLocationServiceOn(false);
		} else {
			MainActivity.startLocationService(this);
			setLocationServiceOn(true);
		}
	}
}
